package com.kuang.quartaz.job;

import com.kuang.order.constant.OrderConstants;
import com.kuang.order.domain.OrderAdopt;
import com.kuang.order.domain.Paybill;
import com.kuang.order.service.OrderAdoptService;
import com.kuang.order.service.PaybillService;
import com.kuang.pet.domain.Pet;
import com.kuang.pet.mapper.PetMapper;
import com.kuang.quartaz.utils.QuartzJobInfo;
import com.kuang.quartaz.utils.QuartzUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class CancelOrder implements Job {
    @Autowired
    private PaybillService paybillService;
    @Autowired
    private OrderAdoptService orderAdoptService;
    @Autowired
    private PetMapper petMapper;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 业务逻辑  到事件结束后执行取消订单
        //  获取动态参数
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        Map<String,Object> params = (Map<String, Object>) jobDataMap.get("params");
        String orderSn = params.get("orderSn").toString();
        // 根据穿的参数订单号去查询paybill
        Paybill paybill = paybillService.loadByOrderSn(orderSn);
        // 修改paybill里面的订单状态
        paybill.setState(-1);
        paybillService.UpdateDepartment(paybill);
        // 根据paybill里面的订单类型去查到是什么订单 通过buskey查找到订单
        switch (paybill.getBusinessType()){
            // 修改查找的订单消息
            case OrderConstants.ORDER_ADOPT:
             OrderAdopt orderAdopt = orderAdoptService.selectDepartmentById(paybill.getBusinessKey());
             orderAdopt.setState(-1);

             orderAdoptService.UpdateDepartment(orderAdopt);
                // 把订单的宠物重新上架
                Pet pet = petMapper.selectDepartmentById(orderAdopt.getPet_id());
                pet.setState(1);
                petMapper.UpdateDepartment(pet);
                break;
            case OrderConstants.ORDER_PRODUCT:
                break;
            case OrderConstants.ORDER_GOODS:
                break;
        }




    }
    public static void CancelJob(Scheduler sched, String jobName){
        QuartzUtils.removeJob(sched,jobName);
    }
}
